package com.a.lib_network.http

import androidx.lifecycle.MutableLiveData
import com.hjq.http.listener.OnHttpListener
import com.a.lib_network.bean.NetState
import com.a.lib_network.enums.NetStateEnum
import okhttp3.Call

/**
 * 将结果通过livedata返回，适用范围广
 * 需要注意livedata的缺点：1.重新绑定时有旧值会通知 2.通知丢失问题
 * https://blog.csdn.net/vivo_tech/article/details/122553764
 * 现已推荐改用MviDispatcher
 * */
abstract class LiveDataHttpCallBack<T>(
    private val liveData: MutableLiveData<NetState>?,
) : OnHttpListener<T> {

    abstract fun succeed(result: T)
    override fun onStart(call: Call?) {
        liveData?.value = NetState(NetStateEnum.START)
    }

    override fun onSucceed(result: T) {
        succeed(result)
    }

    override fun onFail(e: Exception?) {
        liveData?.value = NetState(e.toString(), NetStateEnum.FAIL)
    }

    override fun onEnd(call: Call?) {
        liveData?.value = NetState(NetStateEnum.END)
    }
}